package com.product.android.commonInterface.chat;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.text.TextUtils;
import android.util.Log;

import com.common.android.utils.XMLUtils;

public class ErpInfo implements Serializable{
	private static final long serialVersionUID = 1L;
	private static final String TAG="ErpInfo";
	public static final int TYPE_ALLOCATER=0;		//下单人
	public static final int TYPE_ACCEPTER=1;		//接单人
	public static final int TYPE_COPYTO=2;			//抄送
	
	//用户操作返回结果
	public static final int RESULT_SUCCESS=12;		//成功
	public static final int RESULT_FAIL=13;			//失败
	public static final int RESULT_CANCEL=14;		//用户取消操作
	
	public static final int STATUS_TOACCEPT_TOFINISH=0; 	//接单/结单
	public static final int STATUS_ACCEPTED_FINISHED=1;		//已接单/已结单
	public static final int STATUS_REFUSE_REDO=2;			//拒单/返工
	public static final int STATUS_DETAIL=3;				//仅查看详细
	/**延单（仅下单方）*/
	public static final int STATUS_TODELAY=4;
	/**接受延单（双方）*/
	public static final int STATUS_DELAY_ACCPET=5;
	/**拒绝延单（双方）*/
	public static final int STATUS_DELAY_REFUSE=6;
	
	public String generateId;	//对应的IMS消息的generateId
	public String bussinessId;	//唯一的业务编号
	public String taskId;		//任务编号，对于一个任务的生命周期的各个状态都是一样的
	public String content;		//消息内容
	public String tasktitle;		//消息内容
	public String projectid;		//项目ID
	public String projectname;		//项目名称
	public String appCode;
	public int appId;		
	public int isNeedProject;	//是否需要选择项目 0：不需要 1：需要
	public int type;			//类型：是接单方（1）还是下单方（0）
	public int status=-1;		//消息的状态：0结单/接单 1已结单/已接单 2反工/已拒单 3详情
	public int level=0;			//消息的急紧程度
	public String tasktime;		//任务截止时间 
	public boolean clickable=true;	//按钮是否可点击
	public void parseFromJson(String jsonString) {
		try {
			JSONObject object=new JSONObject(jsonString);
			bussinessId=object.optString("bussinessId");
			taskId=object.optString("taskId");
			content=object.optString("content");
			appCode=object.optString("appCode");
			appId=object.optInt("appId");
			isNeedProject=object.optInt("isNeedProject");
			type=object.optInt("type");
			level=object.optInt("level");
			status=object.optInt("status");
			tasktitle=object.optString("tasktitle");
			projectid=object.optString("projectid");
			projectname=object.optString("projectname");
			tasktime=object.optString("tasktime");
			generateId=object.optString("generateId");
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public String packToJson() {
		JSONObject object=new JSONObject();
		try {
			object.put("bussinessId",bussinessId );
			object.put("taskId",taskId );
			object.put("content",content );
			object.put("appCode",appCode );
			object.put("appId",appId );
			object.put("isNeedProject",isNeedProject );
			object.put("type",type );
			object.put("status",status );
			object.put("tasktitle",tasktitle );
			object.put("projectid",projectid );
			object.put("projectname",projectname );
			object.put("tasktime",tasktime );
			object.put("level",level );
			object.put("generateId",generateId );
			return object.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void parseFromXml(String xmlString) {
		try {
			XmlPullParser xmlPull = XmlPullParserFactory.newInstance().newPullParser();
			InputStream inStream=new ByteArrayInputStream(xmlString.getBytes());
			xmlPull.setInput(inStream, "UTF-8");
			int eventCode = xmlPull.getEventType();
			while (eventCode != XmlPullParser.END_DOCUMENT) {
				switch (eventCode) {
				case XmlPullParser.START_DOCUMENT: 					// 文档开始事件,可以做一些数据初始化处理
					break;
				case XmlPullParser.START_TAG:						// 元素开始.
//					<?xml version="1.0" encoding="utf-8"?>
//					<body fmt="3" type="1" appid="11" permcode="1110" bussid="12345">
//					    <app business="ERP_TASK">
//					        <taskid>ABCD213434</taskid>
//					        <tasktitle>【任务】清完成年终考核中的个人绩效报告</tasktitle>
//					        <tasktime>123821809</tasktime>
//					        <msg>程文渊给您下TODO单,任务内容： 麻烦完善考勤单 完成时间：剩余3天 2013-11-20</msg>
//					        <projectid>0</projectid>
//					        <projectname>应用软件91游</projectname>
//					        <type>0</type>
//					        <status>0</status>
//					    </app>
//					</body>
					
					String name = xmlPull.getName();
					if (name.equalsIgnoreCase("body")){
						appId=XMLUtils.getIntFromXml(xmlPull, "appid");
						bussinessId=xmlPull.getAttributeValue("", "bussid");
					}else if (name.equalsIgnoreCase("app")){
						if (parseMsg(xmlPull)){
							break;
						}else{
							return;
						}
					}
					break;
				case XmlPullParser.END_TAG: 						// 元素结束,
					break;
				}
				eventCode = xmlPull.next();// 进入到一下一个元素.
			}
		} catch (XmlPullParserException e) {
			Log.d(TAG, e.toString());
		} catch (IOException e) {
			Log.d(TAG, e.toString());
		}
	}
	
	private boolean parseMsg(XmlPullParser xmlPull) throws XmlPullParserException, IOException{
		String business=xmlPull.getAttributeValue("", "business");
		if (TextUtils.isEmpty(business) || !business.equals("ERP_TASK")){
			return false;
		}
		int eventCode = xmlPull.next();
		String name=xmlPull.getName();
		while (!(eventCode==XmlPullParser.END_TAG && name.equals("app"))){
			if (name!=null && eventCode!=XmlPullParser.END_TAG ){
				if (name.equalsIgnoreCase("taskid")){
					eventCode=xmlPull.next();
					taskId=xmlPull.getText();
				}else if (name.equalsIgnoreCase("tasktitle")){
					eventCode=xmlPull.next();
					tasktitle=xmlPull.getText();
				}else if (name.equalsIgnoreCase("tasktime")){
					eventCode=xmlPull.next();
					tasktime=xmlPull.getText();
				}else if (name.equalsIgnoreCase("projectid")){
					eventCode=xmlPull.next();
					projectid=xmlPull.getText();
				}else if(name.equalsIgnoreCase("projectname")){
					eventCode=xmlPull.next();
					projectname=xmlPull.getText();
				}else if (name.equalsIgnoreCase("msg")){
					level=XMLUtils.getIntFromXml(xmlPull, "level");
					eventCode=xmlPull.next();
					content=xmlPull.getText();
				}else if (name.equalsIgnoreCase("type")){
					eventCode=xmlPull.next();
					type=Integer.parseInt(xmlPull.getText());
				}else if (name.equalsIgnoreCase("status")){
					eventCode=xmlPull.next();
					status=Integer.parseInt(xmlPull.getText());
				}
			}
			eventCode=xmlPull.next();
			name=xmlPull.getName();
		}
		return true;
	}

}
